package com.hs.jhzd.controller;

import com.hs.jhzd.entity.Configs;
import com.hs.jhzd.entity.Equipment;
import com.hs.jhzd.repository.ConfigsRepository;
import com.hs.jhzd.repository.EquipmentRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/conf")
@Slf4j
public class ConfigsController {

  @Autowired
  private ConfigsRepository repository;

  @Autowired
  private EquipmentRepository equipmentRepository;

  @Value("${jsbh}")
  private String jsbh;

  @RequestMapping("/jsbh")
  @ResponseBody
  public boolean jsbh(HttpServletRequest request) {
    request.getSession().setAttribute("jsbh", this.jsbh);
    return true;
  }

  @RequestMapping("/get")
  @ResponseBody
  public Configs get(HttpServletRequest request, HttpServletResponse response) {
    response.setHeader("Cache-Control", "no-cache");
    List<Configs> configses = repository.findAll();
    Configs configs = CollectionUtils.isEmpty(configses) ? null : configses.get(0);
    if (configs == null) {
      return null;
    }
    boolean inAdress = StringUtils.startsWith(request.getRemoteAddr(), configs.getInAddress())
        && !StringUtils.equals("192.168.1.1", request.getRemoteAddr());
    // log.debug("请求的ip->{} ,内网配置ip->{},是否使用内网地址->{}", request.getRemoteAddr(),
    // configs.getInAddress(),
    // inAdress);
    if (inAdress) {// 内网
      configs.setMqAddress(configs.getMqInAddress());
      configs.setTalkAddress(configs.getTalkInAddress());
      configs.setNginxAddress(configs.getNginxInAddress());
    } else {
      configs.setMqAddress(configs.getMqOutAddress());
      configs.setTalkAddress(configs.getTalkOutAddress());
      configs.setNginxAddress(configs.getNginxOutAddress());
    }
    Equipment equipment =
        equipmentRepository.findByJsbhAndIpAndType(jsbh, request.getRemoteAddr(), "equipment");
    if (equipment != null) {
      configs.setJsh(equipment.getJsh());
      configs.setJsbh(jsbh);
    }
    return configs;
  }

  @RequestMapping("/findByJsbh")
  @ResponseBody
  public Configs findByJsbh(@RequestParam(required = true) String jsbh) {
    List<Configs> configs = repository.findAll();
    if (CollectionUtils.isEmpty(configs)) {
      return null;
    }
    Configs conf = configs.get(0);
    return conf;
  }

  @RequestMapping("/save")
  @ResponseBody
  public boolean save(Configs configs) {
    repository.save(configs);
    return true;
  }

  @RequestMapping("/del")
  @ResponseBody
  public boolean del(Long id) {
    repository.deleteById(id);
    return true;
  }

  @RequestMapping("/admin/main")
  public String editPage(Model model, String jsbh) {
    if (StringUtils.isBlank(jsbh) || StringUtils.equals(jsbh, "null")) {
      return "redirect:/conf/admin/main?jsbh=" + this.jsbh;
    }
    List<Configs> configs = repository.findAll();
    Configs config = CollectionUtils.isEmpty(configs) ? new Configs() : configs.get(0);
    model.addAttribute("jsbh", jsbh);
    model.addAttribute("config", config);
    return "config";
  }
}
